package cn.melonlib.cores.commons.comp.web.ctrl;

import cn.melonlib.cores.aop.comp.web.WebInfo;
import cn.melonlib.cores.jpa.model.bean.collections.JsonMap;
import cn.melonlib.cores.security.auth2.comp.config.ApiConfig;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.annotation.Resource;
import java.io.StringWriter;

@RestController
@RequestMapping(path = "/")
public class IndexController extends BasicController{

    @Value("${server.servlet.context-path}")
    private String base;

    @Resource
    private FreeMarkerConfigurer freeMarkerConfigurer;


    @Resource
    private ApiConfig apiConfig;

    @Resource
    private WebInfo webInfo;

//    @GetMapping(path = "index.html",produces = {"text/html"})
    public String index(String template,JsonMap<Object> params) throws Exception{
        Template index=freeMarkerConfigurer.getConfiguration().getTemplate(template+".html");
        StringWriter sw=new StringWriter();
        String domain=webInfo.getServerInfo().getPrefix().replaceAll("/\\s*$","");
        index.process(params
                ,sw);
        return sw.toString();
    }
}
